package controller;

import model.Card;
import view.UI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

//单牌添加集合是放在中间阶段
public class PlayCardsTimer {
    private Timer timer;
    private ActionListener actionListener;
    private Controller controller;
    private int remainingTime;
    private String noPlay;
    private String eastPlay;
    private String westPlay;
    public String decision;
    public String eastDecision;
    public String westDecison;
    private int currentTimer; // 记录当前倒计时阶段，0表示中间阶段，1表示东部阶段，2表示西部阶段

    public PlayCardsTimer(int delay, Controller controller) {
        UI.enterGameJFrame.enterGameJPanel.openCards.setVisible(false);
        remainingTime = delay;
        this.controller = controller;
        currentTimer = 0;
        actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                decision = UI.enterGameJFrame.enterGameJPanel.soutDeZhuField.getText();
                eastDecision = UI.enterGameJFrame.enterGameJPanel.eastDeZhuField.getText();
                westDecison = UI.enterGameJFrame.enterGameJPanel.westDeZhuField.getText();
                if (decision.equals("地主")) {
                    currentTimer = 0;
                    UI.enterGameJFrame.enterGameJPanel.soutDeZhuField.setText("东家");
                } else if (eastDecision.equals("地主")) {
                    currentTimer = 1;
                    UI.enterGameJFrame.enterGameJPanel.eastDeZhuField.setText("东家");
                } else if (westDecison.equals("地主")) {
                    currentTimer = 2;
                    UI.enterGameJFrame.enterGameJPanel.westDeZhuField.setText("东家");
                }

                if (currentTimer == 0) { // 中间阶段
                    UI.enterGameJFrame.enterGameJPanel.jTextField.setVisible(true);
                    remainingTime--;
                    UI.enterGameJFrame.enterGameJPanel.inDication.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.noPlay.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.yesPlay.setVisible(true);
                    UI.enterGameJFrame.enterGameJPanel.jTextField.setText("倒计时：" + String.valueOf(remainingTime));
                    UI.enterGameJFrame.enterGameJPanel.eastTextField.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.westTextField.setVisible(false);

                    if (remainingTime <= 0) {
                        UI.enterGameJFrame.enterGameJPanel.noPlayLabel.setText("不出");
                        currentTimer = 1;
                        remainingTime = delay;
                        UI.enterGameJFrame.enterGameJPanel.noPlayLabel.setText("");
                        UI.enterGameJFrame.enterGameJPanel.westSoreJLabel.setText("");
                        UI.enterGameJFrame.enterGameJPanel.eastSoreJLabel.setText("");
                    } else if (UI.enterGameJFrame.enterGameJPanel.noPlayLabel.getText().equals("不出")) {
                        // eastComputerPlayCards();
                        currentTimer = 1;
                        remainingTime = delay;
                        UI.enterGameJFrame.enterGameJPanel.noPlayLabel.setText("");
                    } else if (UI.enterGameJFrame.enterGameJPanel.yesPlay.isSelected()) {
                        currentTimer = 1;
                        remainingTime = delay;
                        UI.enterGameJFrame.enterGameJPanel.yesPlay.setSelected(false);
                    }

                } else if (currentTimer == 1) { // 东部阶段
                    eastComputerPlayCards();
                    UI.enterGameJFrame.enterGameJPanel.eastTextField.setVisible(true);
                    remainingTime--;
                    UI.enterGameJFrame.enterGameJPanel.inDication.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.noPlay.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.yesPlay.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.eastTextField.setText("倒计时：" + String.valueOf(remainingTime));
                    UI.enterGameJFrame.enterGameJPanel.jTextField.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.westTextField.setVisible(false);
                    if (remainingTime <= 0) {
                        UI.enterGameJFrame.enterGameJPanel.eastSoreJLabel.setText("不出");
                        currentTimer = 2;
                        remainingTime = delay;

                    }
                } else if (currentTimer == 2) { // 西部阶段
                    westComputerPlayCards();
                    UI.enterGameJFrame.enterGameJPanel.westTextField.setVisible(true);
                    remainingTime--;
                    UI.enterGameJFrame.enterGameJPanel.westTextField.setText("倒计时：" + String.valueOf(remainingTime));
                    UI.enterGameJFrame.enterGameJPanel.jTextField.setVisible(false);
                    UI.enterGameJFrame.enterGameJPanel.eastTextField.setVisible(false);
                    if (remainingTime <= 0) {
                        UI.enterGameJFrame.enterGameJPanel.westSoreJLabel.setText("不出");
                        currentTimer = 0;
                        remainingTime = delay;
                        UI.enterGameJFrame.enterGameJPanel.noPlayLabel.setText("");
                    }
                    //结算界面显示逻辑
                }else if(controller.getEastPcPlayer().list.size()==0&&controller.getPersonPlayer().list.size()!=0&&controller.getWestPcPlayer().list.size()!=0
                        ||controller.getWestPcPlayer().list.size()==0&&controller.getEastPcPlayer().list.size()!=0&&controller.getPersonPlayer().list.size()!=0
                        ||controller.getPersonPlayer().list.size()==0&&controller.getWestPcPlayer().list.size()!=0&&controller.getEastPcPlayer().list.size()!=0){
                   UI.enterGameJFrame.enterGameJPanel.setVisible(false);
                    UI.gameCountJFrame.setVisible(true);
                }
            }
        };

        timer = new Timer(1000, actionListener);
        start();

    }

    public void start() {
        controller.judge = false;
        timer.start();

    }

    public void stop() {
        timer.stop();
    }

    //电脑出牌的逻辑
    public void eastComputerPlayCards() {
        // 东电脑玩家的出牌逻辑
        //单牌出牌
        Card deskCard = controller.deskList.get(controller.deskList.size() - 1);
        ArrayList<Card> tempCards = new ArrayList<>();
        Random random = new Random();

        // 找到符合条件的牌，并将其加入临时列表
        for (Card eastComputerCard : controller.getEastPcPlayer().list) {
            if (eastComputerCard.getNumber() > deskCard.getNumber()||tempCards.isEmpty()) {
                tempCards.add(eastComputerCard);
            }
        }

        // 如果有符合条件的牌
        if (!tempCards.isEmpty()&&controller.deskList.size()==1) {
            if (!tempCards.isEmpty()) {
                // 随机选择一张单牌出
                int index = random.nextInt(tempCards.size());
                Card selectedCard = tempCards.get(index);
                // 从手牌中移除选中的牌
                controller.getEastPcPlayer().list.remove(selectedCard);
                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard);
                currentTimer = 2;


            }


            //出双牌逻辑
        }else if (controller.deskList.size() == 2 && controller.deskList.get(0).getNumber() == controller.deskList.get(1).getNumber() || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            // 找到符合条件的对牌，并将其加入临时列表
            tempCards.clear();
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            for (int i = 0; i < controller.getEastPcPlayer().list.size() - 1; i++) {
                Card card1 = controller.getEastPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 && card1.getNumber() > deskCard2) {
                    for (int j = i + 1; j < controller.getEastPcPlayer().list.size(); j++) {
                        Card card2 = controller.getEastPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 && card2.getNumber() > deskCard2 && card1.getNumber() == card2.getNumber()) {
                            tempCards.add(card1);
                            tempCards.add(card2);

                        } else if (card2.getNumber() < deskCard1 && card2.getNumber() < deskCard2 && card1.getNumber() == card2.getNumber()) {

                        }
                    }
                }
            }

            // 如果有符合条件的对牌
            if (!tempCards.isEmpty()) {
                int index = random.nextInt(tempCards.size() / 2);
                Card selectedCard1 = tempCards.get(index * 2);
                Card selectedCard2 = tempCards.get(index * 2 + 1);

                // 从手牌中移除选中的牌
                controller.getEastPcPlayer().list.remove(selectedCard1);
                controller.getEastPcPlayer().list.remove(selectedCard2);

                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard1);
                controller.deskList.add(selectedCard2);
            }
            currentTimer = 2;
            //出三排的逻辑
        } else if (controller.deskList.size() == 3 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            // 找到符合条件的三张牌，并将其加入临时列表
            tempCards.clear();
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            int deskCard3 = controller.deskList.get(2).getNumber();
            for (int i = 0; i < controller.getEastPcPlayer().list.size() - 2; i++) {
                Card card1 = controller.getEastPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 && card1.getNumber() > deskCard2 && card1.getNumber() > deskCard3) {
                    for (int j = i + 1; j < controller.getEastPcPlayer().list.size() - 1; j++) {
                        Card card2 = controller.getEastPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 && card2.getNumber() > deskCard2 && card2.getNumber() > deskCard3 && card1.getNumber() == card2.getNumber()) {
                            for (int k = j + 1; k < controller.getEastPcPlayer().list.size(); k++) {
                                Card card3 = controller.getEastPcPlayer().list.get(k);
                                if (card3.getNumber() > deskCard1 && card3.getNumber() > deskCard2 && card3.getNumber() > deskCard3 && card1.getNumber() == card3.getNumber()) {
                                    tempCards.add(card1);
                                    tempCards.add(card2);
                                    tempCards.add(card3);
                                }
                            }
                        }
                    }
                }
            }

            // 如果没有符合条件的牌，则不执行逻辑
            if (!tempCards.isEmpty()) {
                int selectIndex = random.nextInt(tempCards.size() / 3) * 3;
                Card selectedCard1 = tempCards.get(selectIndex);
                Card selectedCard2 = tempCards.get(selectIndex + 1);
                Card selectedCard3 = tempCards.get(selectIndex + 2);

                // 从手牌中移除选中的牌
                controller.getEastPcPlayer().list.remove(selectedCard1);
                controller.getEastPcPlayer().list.remove(selectedCard2);
                controller.getEastPcPlayer().list.remove(selectedCard3);

                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard1);
                controller.deskList.add(selectedCard2);
                controller.deskList.add(selectedCard3);

            }
            currentTimer = 2;
            //单顺出牌
        } else if (controller.deskList.size() == 4 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            // 获取桌面上的四张牌的点数
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            int deskCard3 = controller.deskList.get(2).getNumber();
            int deskCard4 = controller.deskList.get(3).getNumber();

            // 找到手牌中符合条件的三张牌
            tempCards.clear();
            for (int i = 0; i < controller.getEastPcPlayer().list.size() - 2; i++) {
                Card card1 = controller.getEastPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 &&
                        card1.getNumber() > deskCard2 &&
                        card1.getNumber() > deskCard3 &&
                        card1.getNumber() > deskCard4) {

                    for (int j = i + 1; j < controller.getEastPcPlayer().list.size() - 1; j++) {
                        Card card2 = controller.getEastPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 &&
                                card2.getNumber() > deskCard2 &&
                                card2.getNumber() > deskCard3 &&
                                card2.getNumber() > deskCard4 &&
                                card1.getNumber() == card2.getNumber()) {

                            for (int k = j + 1; k < controller.getEastPcPlayer().list.size(); k++) {
                                Card card3 = controller.getEastPcPlayer().list.get(k);
                                if (card3.getNumber() > deskCard1 &&
                                        card3.getNumber() > deskCard2 &&
                                        card3.getNumber() > deskCard3 &&
                                        card3.getNumber() > deskCard4 &&
                                        card1.getNumber() == card3.getNumber()) {

                                    tempCards.add(card1);
                                    tempCards.add(card2);
                                    tempCards.add(card3);
                                }
                            }
                        }
                    }
                }
            }

            // 随机选择一组符合条件的三张牌
            int selectIndex = random.nextInt(tempCards.size() / 3) * 3;
            Card selectedCard1 = tempCards.get(selectIndex);
            Card selectedCard2 = tempCards.get(selectIndex + 1);
            Card selectedCard3 = tempCards.get(selectIndex + 2);

            // 在剩余的手牌中随机选择一张单牌
            ArrayList<Card> remainingCards = new ArrayList<>(controller.getEastPcPlayer().list);
            remainingCards.remove(selectedCard1);
            remainingCards.remove(selectedCard2);
            remainingCards.remove(selectedCard3);
            int selectedSingleIndex = random.nextInt(remainingCards.size());
            Card selectedSingleCard = remainingCards.get(selectedSingleIndex);

            // 从手牌中移除选中的牌
            controller.getEastPcPlayer().list.remove(selectedCard1);
            controller.getEastPcPlayer().list.remove(selectedCard2);
            controller.getEastPcPlayer().list.remove(selectedCard3);
            controller.getEastPcPlayer().list.remove(selectedSingleCard);

            // 清空桌面牌，将选中的牌加入桌面牌
            controller.deskList.clear();
            controller.deskList.add(selectedCard1);
            controller.deskList.add(selectedCard2);
            controller.deskList.add(selectedCard3);
            controller.deskList.add(selectedSingleCard);
            currentTimer = 2;
            //炸弹出牌
        } else if (controller.deskList.size() == 4 && controller.deskList.get(0).getNumber() == controller.deskList.get(1).getNumber()
                && controller.deskList.get(0).getNumber() == controller.deskList.get(2).getNumber() && controller.deskList.get(0).getNumber() == controller.deskList.get(3).getNumber()
                && controller.deskList.get(1).getNumber() == controller.deskList.get(2).getNumber() && controller.deskList.get(1).getNumber() == controller.deskList.get(3).getNumber()
                && controller.deskList.get(2).getNumber() == controller.deskList.get(3).getNumber()) {
            // 查找手牌中的所有炸弹
            ArrayList<ArrayList<Card>> bombList = new ArrayList<>();
            for (int i = 0; i < controller.getEastPcPlayer().list.size(); i++) {
                Card card1 = controller.getEastPcPlayer().list.get(i);
                if (card1.getNumber() == controller.getEastPcPlayer().list.get(i).getNumber()) {
                    for (int j = i + 1; j < controller.getEastPcPlayer().list.size(); j++) {
                        Card card2 = controller.getEastPcPlayer().list.get(j);
                        if (card2.getNumber() == card1.getNumber()) {
                            for (int k = j + 1; k < controller.getEastPcPlayer().list.size(); k++) {
                                Card card3 = controller.getEastPcPlayer().list.get(k);
                                if (card3.getNumber() == card2.getNumber()) {
                                    for (int l = k + 1; l < controller.getEastPcPlayer().list.size(); l++) {
                                        Card card4 = controller.getEastPcPlayer().list.get(l);
                                        if (card4.getNumber() == card3.getNumber()) {
                                            ArrayList<Card> bomb = new ArrayList<>();
                                            bomb.add(card1);
                                            bomb.add(card2);
                                            bomb.add(card3);
                                            bomb.add(card4);
                                            bombList.add(bomb);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // 随机选择一组炸弹
            int selectIndex = random.nextInt(bombList.size());
            ArrayList<Card> selectedBomb = bombList.get(selectIndex);

            // 从手牌中移除选中的牌
            controller.getEastPcPlayer().list.removeAll(selectedBomb);

            // 清空桌面牌，将选中的炸弹加入桌面牌
            controller.deskList.clear();
            controller.deskList.addAll(selectedBomb);
            currentTimer = 2;
        }


    }


    public void westComputerPlayCards() {

            // 西电脑玩家的出牌逻辑
            Card deskCard = controller.deskList.get(controller.deskList.size() - 1);
            Random random = new Random();
            ArrayList<Card> tempCards = new ArrayList<>();
            // 找到符合条件的牌，并将其加入临时列表
            for (Card westComputerCard : controller.getWestPcPlayer().list) {
                if (westComputerCard.getNumber() > deskCard.getNumber()) {
                    tempCards.add(westComputerCard);
                }
            }



        // 如果有符合条件的牌
        if (!tempCards.isEmpty() && controller.deskList.size() == 1 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            if(!tempCards.isEmpty()){
                //出单牌的逻辑
                int selectIndex = random.nextInt(tempCards.size());
                Card selectedCard = tempCards.get(selectIndex);

                // 从手牌中移除选中的牌
                controller.getWestPcPlayer().list.remove(selectedCard);

                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard);
                currentTimer = 0;
            }


            //出双牌逻辑
        } else if (controller.deskList.size() == 2 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {

            // 找到符合条件的对牌，并将其加入临时列表
            tempCards.clear();
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            for (int i = 0; i < controller.getWestPcPlayer().list.size() - 1; i++) {
                Card card1 = controller.getWestPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 && card1.getNumber() > deskCard2) {
                    for (int j = i + 1; j < controller.getWestPcPlayer().list.size(); j++) {
                        Card card2 = controller.getWestPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 && card2.getNumber() > deskCard2 && card1.getNumber() == card2.getNumber()) {
                            tempCards.add(card1);
                            tempCards.add(card2);
                        } else if (card2.getNumber() < deskCard1 && card2.getNumber() < deskCard2 && card1.getNumber() == card2.getNumber()) {
                            UI.enterGameJFrame.enterGameJPanel.westSoreJLabel.setText("不要");
                        }
                    }
                }
            }

            // 如果有符合条件的对牌
            if (!tempCards.isEmpty()) {
                int index = random.nextInt(tempCards.size() / 2);
                Card selectedCard1 = tempCards.get(index * 2);
                Card selectedCard2 = tempCards.get(index * 2 + 1);

                // 从手牌中移除选中的牌
                controller.getWestPcPlayer().list.remove(selectedCard1);
                controller.getWestPcPlayer().list.remove(selectedCard2);

                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard1);
                controller.deskList.add(selectedCard2);
            }
            currentTimer = 0;
            //出三张牌的逻辑
        } else if (controller.deskList.size() == 3 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            // 找到符合条件的三张牌，并将其加入临时列表
            tempCards.clear();
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            int deskCard3 = controller.deskList.get(2).getNumber();
            for (int i = 0; i < controller.getWestPcPlayer().list.size() - 2; i++) {
                Card card1 = controller.getWestPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 && card1.getNumber() > deskCard2 && card1.getNumber() > deskCard3) {
                    for (int j = i + 1; j < controller.getWestPcPlayer().list.size() - 1; j++) {
                        Card card2 = controller.getWestPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 && card2.getNumber() > deskCard2 && card2.getNumber() > deskCard3 && card1.getNumber() == card2.getNumber()) {
                            for (int k = j + 1; k < controller.getWestPcPlayer().list.size(); k++) {
                                Card card3 = controller.getWestPcPlayer().list.get(k);
                                if (card3.getNumber() > deskCard1 && card3.getNumber() > deskCard2 && card3.getNumber() > deskCard3 && card1.getNumber() == card3.getNumber()) {
                                    tempCards.add(card1);
                                    tempCards.add(card2);
                                    tempCards.add(card3);
                                }
                            }
                        }
                    }
                }
            }

            // 如果没有符合条件的牌，则不执行逻辑
            if (!tempCards.isEmpty()) {
                int selectIndex = random.nextInt(tempCards.size() / 3) * 3;
                Card selectedCard1 = tempCards.get(selectIndex);
                Card selectedCard2 = tempCards.get(selectIndex + 1);
                Card selectedCard3 = tempCards.get(selectIndex + 2);

                // 从手牌中移除选中的牌
                controller.getWestPcPlayer().list.remove(selectedCard1);
                controller.getWestPcPlayer().list.remove(selectedCard2);
                controller.getWestPcPlayer().list.remove(selectedCard3);

                // 清空桌面牌，将选中的牌加入桌面牌
                controller.deskList.clear();
                controller.deskList.add(selectedCard1);
                controller.deskList.add(selectedCard2);
                controller.deskList.add(selectedCard3);

            }
            currentTimer = 2;

        } else if (controller.deskList.size() == 4 || UI.enterGameJFrame.enterGameJPanel.noPlayLabel.equals("不出")) {
            // 获取桌面上的四张牌的点数
            int deskCard1 = controller.deskList.get(0).getNumber();
            int deskCard2 = controller.deskList.get(1).getNumber();
            int deskCard3 = controller.deskList.get(2).getNumber();
            int deskCard4 = controller.deskList.get(3).getNumber();

            // 找到手牌中符合条件的三张牌
            tempCards.clear();
            for (int i = 0; i < controller.getWestPcPlayer().list.size() - 2; i++) {
                Card card1 = controller.getEastPcPlayer().list.get(i);
                if (card1.getNumber() > deskCard1 &&
                        card1.getNumber() > deskCard2 &&
                        card1.getNumber() > deskCard3 &&
                        card1.getNumber() > deskCard4) {

                    for (int j = i + 1; j < controller.getWestPcPlayer().list.size() - 1; j++) {
                        Card card2 = controller.getWestPcPlayer().list.get(j);
                        if (card2.getNumber() > deskCard1 &&
                                card2.getNumber() > deskCard2 &&
                                card2.getNumber() > deskCard3 &&
                                card2.getNumber() > deskCard4 &&
                                card1.getNumber() == card2.getNumber()) {

                            for (int k = j + 1; k < controller.getWestPcPlayer().list.size(); k++) {
                                Card card3 = controller.getWestPcPlayer().list.get(k);
                                if (card3.getNumber() > deskCard1 &&
                                        card3.getNumber() > deskCard2 &&
                                        card3.getNumber() > deskCard3 &&
                                        card3.getNumber() > deskCard4 &&
                                        card1.getNumber() == card3.getNumber()) {

                                    tempCards.add(card1);
                                    tempCards.add(card2);
                                    tempCards.add(card3);
                                }
                            }
                        }
                    }
                }
            }

            // 随机选择一组符合条件的三张牌
            int selectIndex = random.nextInt(tempCards.size() / 3) * 3;
            Card selectedCard1 = tempCards.get(selectIndex);
            Card selectedCard2 = tempCards.get(selectIndex + 1);
            Card selectedCard3 = tempCards.get(selectIndex + 2);

            // 在剩余的手牌中随机选择一张单牌
            ArrayList<Card> remainingCards = new ArrayList<>(controller.getWestPcPlayer().list);
            remainingCards.remove(selectedCard1);
            remainingCards.remove(selectedCard2);
            remainingCards.remove(selectedCard3);
            int selectedSingleIndex = random.nextInt(remainingCards.size());
            Card selectedSingleCard = remainingCards.get(selectedSingleIndex);

            // 从手牌中移除选中的牌
            controller.getWestPcPlayer().list.remove(selectedCard1);
            controller.getWestPcPlayer().list.remove(selectedCard2);
            controller.getWestPcPlayer().list.remove(selectedCard3);
            controller.getWestPcPlayer().list.remove(selectedSingleCard);

            // 清空桌面牌，将选中的牌加入桌面牌
            controller.deskList.clear();
            controller.deskList.add(selectedCard1);
            controller.deskList.add(selectedCard2);
            controller.deskList.add(selectedCard3);
            controller.deskList.add(selectedSingleCard);
            currentTimer = 0;
            
            //出炸逻辑
        } else if (controller.deskList.size() == 4 && controller.deskList.get(0).getNumber() == controller.deskList.get(1).getNumber()
                && controller.deskList.get(0).getNumber() == controller.deskList.get(2).getNumber() && controller.deskList.get(0).getNumber() == controller.deskList.get(3).getNumber()
                && controller.deskList.get(1).getNumber() == controller.deskList.get(2).getNumber() && controller.deskList.get(1).getNumber() == controller.deskList.get(3).getNumber()
                && controller.deskList.get(2).getNumber() == controller.deskList.get(3).getNumber()) {
            // 查找手牌中的所有炸弹
            ArrayList<ArrayList<Card>> bombList = new ArrayList<>();
            for (int i = 0; i < controller.getWestPcPlayer().list.size(); i++) {
                Card card1 = controller.getWestPcPlayer().list.get(i);
                if (card1.getNumber() == controller.getWestPcPlayer().list.get(i).getNumber()) {
                    for (int j = i + 1; j < controller.getWestPcPlayer().list.size(); j++) {
                        Card card2 = controller.getWestPcPlayer().list.get(j);
                        if (card2.getNumber() == card1.getNumber()) {
                            for (int k = j + 1; k < controller.getWestPcPlayer().list.size(); k++) {
                                Card card3 = controller.getWestPcPlayer().list.get(k);
                                if (card3.getNumber() == card2.getNumber()) {
                                    for (int l = k + 1; l < controller.getWestPcPlayer().list.size(); l++) {
                                        Card card4 = controller.getWestPcPlayer().list.get(l);
                                        if (card4.getNumber() == card3.getNumber()) {
                                            ArrayList<Card> bomb = new ArrayList<>();
                                            bomb.add(card1);
                                            bomb.add(card2);
                                            bomb.add(card3);
                                            bomb.add(card4);
                                            bombList.add(bomb);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // 随机选择一组炸弹
            int selectIndex = random.nextInt(bombList.size());
            ArrayList<Card> selectedBomb = bombList.get(selectIndex);

            // 从手牌中移除选中的牌
            controller.getWestPcPlayer().list.removeAll(selectedBomb);

            // 清空桌面牌，将选中的炸弹加入桌面牌
            controller.deskList.clear();
            controller.deskList.addAll(selectedBomb);
            currentTimer = 0;
        }
    }
}






